package com.example.healthcondition.controller;


import com.example.healthcondition.entity.HealthData;
import com.example.healthcondition.services.HealthDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/login/home/healthData")
public class HealthDataController {

    private final HealthDataService healthDataService;

    @Autowired
    public HealthDataController(HealthDataService healthDataService) {
        this.healthDataService = healthDataService;
    }

    @GetMapping("/latest/{userId}")
    public ResponseEntity<HealthData> getLatestHealthData(@PathVariable Integer userId) {
        HealthData healthData = healthDataService.findLatestByUserId(userId);
        return ResponseEntity.ok(healthData);
    }

    @PostMapping("/add")
    public ResponseEntity<Integer> addHealthData(@RequestBody HealthData healthData) {
        int result = healthDataService.insertHealthData(healthData);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/modify")
    public ResponseEntity<Integer> updateHealthData(@RequestBody HealthData healthData) {
        int result = healthDataService.updateHealthData(healthData);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/{dataId}")
    public ResponseEntity<HealthData> getHealthDataById(@PathVariable Integer dataId) {
        HealthData healthData = healthDataService.findById(dataId);
        return ResponseEntity.ok(healthData);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<HealthData>> getAllHealthDataByUserId(@PathVariable Integer userId) {
        List<HealthData> healthDataList = healthDataService.findAllByUserId(userId);
        return ResponseEntity.ok(healthDataList);
    }
}